package com.iweb.dao;

import com.iweb.db.XdDataSource;
import com.iweb.pojo.CommodityPic;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CommodityPicDao {

    // 图片的根据商品id进行查询
    public CommodityPic findByCommodityId(String commodityId) {
        Connection conn = XdDataSource.getConnection();
        CommodityPic commodityPic = null;
        try {
            String sql = "select * from xd_commodity_pic where COMMODITY_ID = ? and state = ?";

            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1, commodityId);
            pst.setString(2, "active");
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                commodityPic = createCommodityPic(rs);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return commodityPic;
    }

    private CommodityPic createCommodityPic(ResultSet rs) throws SQLException {
        CommodityPic pic = new CommodityPic();
        return pic.set(
                rs.getString(1),
                rs.getString(2),
                rs.getString(3),
                rs.getString(4)
        );
    }

}
